package com.clstu.sellTicket;

import java.util.TreeSet;

public class SellTicket {
    public static void main(String[] args) {
        new Sell().start();//Thread 实现多线程
        new Sell().start();
        new Sell().start();

        //实现Runnable来实现多线程
//        Sell01 sell01 = new Sell01();//不论如何都会发生多卖票的现象，超卖
//        new Thread(sell01).start();
//        new Thread(sell01).start();
//        new Thread(sell01).start();
    }
}

class Sell extends Thread{//Tread实现多线程
    public static int num = 100;
    int count = 0;
    @Override
    public void run() {
        int i = 0;
        while (num>0){
            System.out.println("窗口=="+Thread.currentThread().getName()+"  售出一张票，还剩票数  "+(--num));
            count++;
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("窗口=="+Thread.currentThread().getName()+" 总共售出票数： "+count);
    }
}

class Sell01 implements Runnable{
    public int num = 100;
    int count = 0;
    @Override
    public void run() {
        int i=0;
        while (num>0){
            System.out.println("窗口=="+Thread.currentThread().getName()+"  售出一张票，还剩票数  "+(--num));
            System.out.println(" 总共售出票数： "+count);
            count++;
            i++;
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(" 总共售出票数： "+count+"   i:"+(i++));//因为线程冲突问题，统计也不准确，
    }
}